home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6939 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: watnews.watson.ibm.com!ncohen
  2. From: ncohen@watson.ibm.com (Norman H. Cohen)
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: on OO differnces between Ada95 and C++
  5. Date: 20 Feb 1996 22:36:35 GMT
  6. Organization: IBM T.J. Watson Research Center
  7. Distribution: world
  8. Message-ID: <4gdidj$10f5@watnews1.watson.ibm.com>
  9. References: <4gbq7q$g08@qualcomm.com>
  10. Reply-To: ncohen@watson.ibm.com
  11. NNTP-Posting-Host: rios8.watson.ibm.com
  12.  
  13. In article <4gbq7q$g08@qualcomm.com>, nabbasi@qualcomm.com (Nasser Abbasi)
  14. observes that in C and C++ one specifies an interface in a .h (or .H)
  15. file and imports the interface with a #include directive, while in Ada
  16. one specifies an interface with a program-unit declaration (typically a
  17. package declaration) and imports it with a with clause.  In C and C++
  18. importing is transitive--if B #includes A and C #includes B, then C has
  19. effectively #included--while in Ada it is not--if B has a with clause for
  20. A and C has a with clause for B, then A is not visible in C unless C has
  21. its own explicit with clause for A.
  22.  
  23. I prefer the Ada model for two reasons: 
  24.  
  25. 1.  The Ada model provides a more precise description of which library
  26.     units are visible where.  A library unit is visible only in
  27.     compilation units naming it in a with clause.  Some Ada programmers
  28.     use subunits to make this information even more precise.  For
  29.     example, if the only place in a large package body where Ada.Text_IO
  30.     is used is in one particular procedure body, that procedure body may
  31.     be broken off into a subunit and the with clause for Ada.Text_IO
  32.     placed on the subunit rather than the package body, to make clear to
  33.     readers that Ada.Text_IO is only used in that one procedure.
  34.  
  35. 2.  In a compilation unit U that refers to an imported entity X, the Ada
  36.     model makes it easier to find the unit exporting X.  It must be a
  37.     unit named in a with clause on U, on the corresponding declaration if
  38.     U is a body, or a parent unit.
  39.  
  40. When the programmers deem it appropriate, it is possible for one package
  41. to reexport entities declared in another package using renaming, subtype,
  42. and number declarations: 
  43.  
  44.    package A is
  45.       type T is ...;
  46.       procedure P(X: in out T);
  47.       Y: constant T := ...;
  48.       ...
  49.    end A;
  50.  
  51.    with A;
  52.    package B is
  53.       subtype T is A.T;
  54.       procedure P(X: in out T) renames A.P;
  55.       Y: constant T := A.C;
  56.       ...
  57.    end B;
  58.  
  59. However, reexport is not forced on the programmer as in the case of a .h
  60. file that #includes another .h file.  In the example above, if a
  61. compilation unit C has a with clause for B and refers to T, P, or Y, the
  62. reader for C can refer to the declaration of B to find declarations for
  63. T, P, and Y.
  64.  
  65. In OOP, it is often desirable for entities declared along with a parent
  66. type P to be reexported along with any type D derived from P.  This can
  67. be achieved automatically by deriving from P in a child of the package in
  68. which D is declared: 
  69.  
  70.    package Parent is
  71.       type Auxiliary_Type is ...;
  72.       type P is tagged ...;
  73.       procedure Op (X: in out P; Y: in Auxiliary_Type);
  74.       ...
  75.    end Parent;
  76.  
  77.    package Parent.Child is
  78.       type D is new P with ...;
  79.       procedure Op (X: in out D; Y: in Auxiliary_Type);
  80.       ...
  81.    end Parent.Child;
  82.  
  83. A compilation unit that mentions Parent.Child in a with clause is, in
  84. effect, really importing a version of Parent with Parent.Child nested
  85. inside of it, so the declaration of Auxiliary_Type comes along.
  86.  
  87. --
  88. Norman H. Cohen    ncohen@watson.ibm.com
  89.